# -*- coding: utf-8 -*-
# @Software :   PyCharm
# @File     :   tasks.py
# @Author   :   zhaolie
# @Contact  :   zhaolie1989@gmail.com
# @Time     :   2019/11/17 8:43 下午
# @Desc     :   None      

# here put the import lib
from django.core.mail import send_mail
from django.conf import settings
from celery import Celery

# django环境的初始化，在任务处理者worker一端加以下几句
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mxonline.settings')
django.setup()

# 创建一个Celery类的实例对象
app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1')


# 定义任务函数
@app.task
def send_register_active_email(to_email, username, token, type):
    # 发送激活邮件,组织邮件信息
    if type == 'register':
        subject = 'MxOnline注册激活链接'
        html_message = '<h1>%s, 欢迎您成为MxOnline注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/active/%s">http://127.0.0.1:8000/active/%s</a>' % (
            username, token, token)
    else:
        subject = 'MxOnline重置密码链接'
        html_message = '<h1>%s</h1>请点击下面链接重置您的密码<br/><a href="http://127.0.0.1:8000/reset/%s">http://127.0.0.1:8000/reset/%s</a>' % (
            username, token, token)

    message = ''
    sender = settings.EMAIL_FROM  # 发送人
    receiver = [to_email]
    send_mail(subject, message, sender, receiver, html_message=html_message)
